<?php
/**
 * 商户接口
 * @author 孙银川
 * @date   2018.1.5
 */
namespace app\api\controller;

use app\common\model\Shop as ShopModel;
use app\common\model\Goods as GoodsModel;
use app\common\model\ShopGcate;
use app\common\model\Shops;

class Shop extends Base{

	/**
     * 获取商户分类
     */
	public function shopCate(){
		$smodel = new ShopModel();
		$map[] = ['ty_status','=',1];
		$data = $smodel->cates();
		foreach($data as $k=>$v){
			if($v['ty_ico']) $data[$k]['ty_ico'] = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].'/'.$v['ty_ico'];
		}
		return $this->getOutput(200,'ok',$data);
	}

	/**
     * 获取商户分类下的所有商户
     */
	public function shops(){
		// 分类ID
		$ty_id = $this->request->param('ty_id');
		if($ty_id){
			$smodel = new ShopModel();
			$data = $smodel->cate_shops($ty_id);
			foreach($data as $k=>$v){
				if($v['sp_logo_url']) $data[$k]['sp_logo_url'] = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].'/'.$v['sp_logo_url'];
			}
			return $this->getOutput(200,'ok',$data);
		}else{
			return $this->getOutput(404,'获取失败');
		}
	}

	/**
     * 商户首页
     */
	public function shopGoods(){
        // 商品ID
        $shop_id = $this->request->param('shop_id');//商户Id
        if($shop_id) {
            $shopInfo = Shops::where('sp_id', $shop_id)->field('sp_name,sp_logo_url,sp_round_url,sp_id,sp_person_flow,sp_start_time,sp_end_time,sp_address')->find();
            // 店铺LOGO
            if($shopInfo['sp_logo_url']) $shopInfo['sp_logo_url'] = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].'/'.$shopInfo['sp_logo_url'];
            // 店铺轮播
            if($shopInfo['sp_round_url']){
            	$round = explode(',',$shopInfo['sp_round_url']);
            	foreach($round as $k=>$v){
            		$round[$k] = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].'/'.$v;
            	}
            	$shopInfo['sp_round_url'] = $round;
            }

            $recommend = GoodsModel::where('gd_shop_id',$shop_id)->field("gd_id,gd_name,gd_image_url,gd_price,gd_shop_id")->where('gd_status=1 and gd_audit=1')->limit(2)->select();
            foreach($recommend as $k=>$v){
	        	if($v['gd_image_url']) $recommend[$k]['gd_image_url'] = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].'/'.$v['gd_image_url'];
	        	if($v['gd_price']) $recommend[$k]['gd_price'] = $v['gd_price']/100;
	        }
            return $this->getOutput(200,'ok',array('shopInfo'=>$shopInfo,'recommend'=>$recommend));
        }else{
            return $this->getOutput(404,'获取失败');
        }
    }

	/**
     * 获取商户详情
     */
	public function goods(){
		// 商品ID
		$goods_id = $this->request->param('goods_id');
		if($goods_id){
			$gmodel = new GoodsModel();
			$data = $gmodel->detail($goods_id);
			$data['gd_price'] = $data['gd_price']/100;
			if($data['gd_image_url']) $data['gd_image_url'] = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].'/'.$data['gd_image_url'];
			return $this->getOutput(200,'ok',$data);
		}else{
			return $this->getOutput(404,'获取失败');
		}
	}

    /**
     * 获取商户所有产品
     */
	public function all_goods_shops(){
	    $shop_id = $this->request->param('shop_id');
	    $shop_type = $this->request->param('shop_type',0);
	    if($shop_id){
	        $gmodel = new GoodsModel();
	        $gcate = ShopGcate::where('sg_shop_id',$shop_id)->where('sg_status',1)->order('sg_sort','desc')->field('sg_id,sg_name')->select();
	        $data[] = ['gd_audit','=',1];
	        $data[] = ['gd_status','=',1];
            if($shop_type) $data[] = ['gd_cate_id','=',$shop_type];
	        $goods = $gmodel->where('gd_shop_id',$shop_id)->where($data)->field("gd_id,gd_name,gd_image_url,gd_price")->select();
	        foreach($goods as $k=>$v){
	        	if($v['gd_image_url']) $goods[$k]['gd_image_url'] = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].'/'.$v['gd_image_url'];
	        	if($v['gd_price']) $goods[$k]['gd_price'] = $v['gd_price']/100;
	        }
            return $this->getOutput(200,'ok',array('gcate'=>$gcate,'goods'=>$goods));
        }else{
	        return $this->getOutput(4003,'获取失败');
        }
    }
}
?>